<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use Markdown;
use App\Model\Art;
use Auth;

use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

use Captcha;

class ArtController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	//验证码
	public function getCode()
	{
		echo captcha_src();
	}
	public function getVc()
	{
		// echo Captcha::generate();
		dd(Captcha::check('f4bj'));
	}
    public function index()
    {
        $arts = DB::table('art')->orderBy('id','desc')->paginate(5);
        return view('admin.Index',['arts'=>$arts]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.Add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $title = $request->title;
        $content = $request->content;
        $data = ['title'=>$title,'content'=>$content];
        $res = DB::table('art')->insert($data);
        if($res){
            return redirect('/admin');
        }
        else{
            return back();
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
         $arts = DB::table("art")->where("id",$id)->first();
          $arts->title = Markdown::convertToHtml(strip_tags($arts->title));
          $arts->content = Markdown::convertToHtml(strip_tags($arts->content));
         return view("admin.Show",['arts'=>$arts]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        
        $arts = DB::table("art")->where("id",$id)->first();
        return view('admin.Edit',['arts'=>$arts]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $res = ['title' => $request->title,'content' => $request->content];
       $arts = DB::table("art")->where("id",$id)->update($res);
      return redirect('/admin');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $arts=DB::table('art')->where('id',$id)->delete();
        return redirect('/admin');
    }
    public function getAbc()
    {
    	dd(Auth::user);
    }
    public function getMk()
    {
    	$str="<p>#    h1</p>";
    	echo Markdown::convertToHtml($str);
    }
    
    public  function  getUser()
    {
    	dd(Auth::user());
    
    	//dd(Auth::check());
    
    }
    public function  getLogout()
    {
    	Auth::logout();
    }
    
    public  function postLogin(Request $request)
    {
    
    	$res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);
    
    	if($res){
    		return redirect('/admin');
    	}else{
    		return back();
    	}
    }
    
    
    
}






















